module game {
    /**
     * 玩家登录页
     * 
    */
    export class PlayerLoginMediator extends BaseMediator {
        public static NAME: string = "PlayerLoginMediator";

        public constructor(viewComponent: any = null) {
            super(PlayerLoginMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.LOGIN.OPEN_PLAYERLOGIN,        //打开玩家登录页
                PanelNotify.LOGIN.CLOSE_PLAYERLOGIN,       //关闭玩家登录页
            ];
        }
        private PlayerLoginPanel: PlayerLoginPanel;
        private ViewStackIndex = 0;
        public handleNotification(notification: puremvc.INotification): void {
            var data: any = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.PlayerLoginPanel) {
                        this.PlayerLoginPanel.width = GameConfig.curWidth()
                        this.PlayerLoginPanel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.LOGIN.OPEN_PLAYERLOGIN:
                    this.PlayerLoginPanel = null;
                    this.PlayerLoginPanel = new PlayerLoginPanel();
                    this.showUI(this.PlayerLoginPanel, 2, 1, true);
                    break;
                case PanelNotify.LOGIN.CLOSE_PLAYERLOGIN:
                    this.closePanel(1);
                    break;
            }
        }

        public initUI(): void {
            this.PlayerLoginPanel.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseBtnClick, this);
            this.PlayerLoginPanel.btn_forgetpss.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onForgetBtnClick, this);
            this.PlayerLoginPanel.login_dl.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLoginBtnClick, this);
            this.PlayerLoginPanel.img_res.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRegisteBtnClick, this);
            this.PlayerLoginPanel.login_gou.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickRemindedPass, this);
            this.PlayerLoginPanel.lab_code.addEventListener(egret.TouchEvent.FOCUS_IN, this.handleFocusIn, this);
            this.PlayerLoginPanel.lab_phonenum.addEventListener(egret.TouchEvent.FOCUS_IN, this.handleFocusIn, this);
            this.PlayerLoginPanel.login_gouk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickRemindedPass, this);

            this.PlayerLoginPanel.lab_code.inputType = egret.TextFieldInputType.PASSWORD;
            this.PlayerLoginPanel.lab_code.displayAsPassword = true;

            let userId = egret.localStorage.getItem(InstructionsEnum.USERID);
            let password = egret.localStorage.getItem(InstructionsEnum.USERPASSWORD);
            // let tmppass =  
            if (password != "null" && password) {
                this.PlayerLoginPanel.login_gou.visible = true;
                this.PlayerLoginPanel.lab_code.text = password;
            }
            else {
                this.PlayerLoginPanel.lab_code.text = "";
            }
            if (userId != "null" && userId) {
                this.PlayerLoginPanel.lab_phonenum.text = userId;
            }
            else {
                this.PlayerLoginPanel.lab_phonenum.text = "";
            }

        }

        //关闭登录页按钮点击事件
        private onCloseBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget); this.closePanel(1);
        }

        //客服按钮点击事件
        private onCustomerServiceBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            // game.AppFacade.instance.sendNotification(PanelNotify.OPEN_CUSTOMER_SERVICE);
        }

        //登录按钮点击事件
        private onLoginBtnClick(event: egret.TouchEvent): void {
            var __this = this;
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            let phone = this.PlayerLoginPanel.lab_phonenum.text;
            let pass = this.PlayerLoginPanel.lab_code.text;
            if (Global.isEmpty(phone)) {
                Global.tipShow("请输入账号!");
                return;
            }
            if (Global.isEmpty(pass)) {
                Global.tipShow("请输入密码!");
                return;
            }
            // obj["wModuleID"] = 123;
            // obj["dwPlazaVersion"] = 101122051; //101056515
            // obj["cbDeviceType"] = 2;    //1,安卓 2,ios 3,pc
            EffectUtils.showWaritPanel();

            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/H5Interface/H5UserLogin?UserAccount=" + phone + "&UserPassword=" + new utils.MD5().hex_md5(pass).toUpperCase() +
                "&Token=" + new utils.MD5().hex_md5(phone + "NH11MqbKJ5hVr9do").toUpperCase() + "&PlatForm=yuezhong", egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.hideWaritPanel();
                        let mess = JSON.parse(obj.mess)
                        let objc = {
                            dwUserName: mess.username,
                            platform: mess.platform,
                            token: mess.token
                        }
                        if (this.PlayerLoginPanel.login_gou.visible == true) {
                            egret.localStorage.setItem(InstructionsEnum.USERPASSWORD, pass);
                            egret.localStorage.setItem(InstructionsEnum.USERID, phone);
                        }
                        GameVO.getVo().szPassword = this.PlayerLoginPanel.lab_code.text;
                        GameVO.getVo().szMobilePhone = phone;
                        InstructionsEnum.UserName = objc.dwUserName;
                        InstructionsEnum.platForm = objc.platform;
                        InstructionsEnum.token = objc.token;
                        InstructionsEnum.isGuest = "0";
                        SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.CMD_MB_LogonUserApi, objc, true);
                        __this.closePanel(1)
                    }
                    else {
                        EffectUtils.hideWaritPanel();
                        Global.tipShow(obj.mess)
                    }
                } catch (err) {
                    EffectUtils.hideWaritPanel();
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            // SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.CMD_MB_LogonUserApi, obj, true);
            EffectUtils.showWaritPanel();
            GameVO.getVo().bTourist = false;
        }

        private onRegisteBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            game.AppFacade.instance.sendNotification(PanelNotify.LOGIN.OPEN_PLAYERREGISTER);
        }
        private onForgetBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            game.AppFacade.instance.sendNotification(PanelNotify.LOGIN.OPEN_PLAYFORGET);
        }
        private clickRemindedPass(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            let phone = this.PlayerLoginPanel.lab_phonenum.text;
            let pass = this.PlayerLoginPanel.lab_code.text;
            if (egret.localStorage.getItem(InstructionsEnum.USERPASSWORD) || this.PlayerLoginPanel.login_gou.visible == true) {
                this.PlayerLoginPanel.login_gou.visible = false;
                egret.localStorage.removeItem(InstructionsEnum.USERPASSWORD);
                egret.localStorage.removeItem(InstructionsEnum.USERID);
            }
            else if (this.PlayerLoginPanel.login_gou.visible == false) {
                this.PlayerLoginPanel.login_gou.visible = true
            }


        }
        private handleFocusIn(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            egret.localStorage.removeItem(InstructionsEnum.USERPASSWORD);
            egret.localStorage.removeItem(InstructionsEnum.USERID);
        }


    }
}